<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="缺陷记录ID" prop="defectRecordId">
        <el-input
          v-model="queryParams.defectRecordId"
          placeholder="请输入缺陷记录ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择工单状态" clearable>
          <el-option label="待处理" value="待处理" />
          <el-option label="处理中" value="处理中" />
          <el-option label="已完成" value="已完成" />
          <el-option label="已取消" value="已取消" />
        </el-select>
      </el-form-item>
      <el-form-item label="分配给" prop="assignTo">
        <el-input
          v-model="queryParams.assignTo"
          placeholder="请输入分配人员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修类型" prop="repairType">
        <el-select v-model="queryParams.repairType" placeholder="请选择维修类型" clearable>
          <el-option label="常规维修" value="常规维修" />
          <el-option label="紧急维修" value="紧急维修" />
          <el-option label="预防性维修" value="预防性维修" />
        </el-select>
      </el-form-item>
      <el-form-item label="维修级别" prop="repairLevel">
        <el-select v-model="queryParams.repairLevel" placeholder="请选择维修级别" clearable>
          <el-option label="轻度" value="轻度" />
          <el-option label="中度" value="中度" />
          <el-option label="重度" value="重度" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
        <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          :icon="Plus"
          @click="handleAdd"
          v-hasPermi="['qep:repair-order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          :icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qep:repair-order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          :icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qep:repair-order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          :icon="Download"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="工单编号" align="center" prop="orderNo" />
      <el-table-column label="缺陷记录ID" align="center" prop="defectRecordId" />
      <el-table-column label="工单状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === '待处理' ? 'info' : 
                        scope.row.status === '处理中' ? 'warning' :
                        scope.row.status === '已完成' ? 'success' : 'danger'">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分配给" align="center" prop="assignTo" />
      <el-table-column label="维修类型" align="center" prop="repairType" />
      <el-table-column label="维修级别" align="center" prop="repairLevel" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际日期" align="center" prop="actualDate" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.actualDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修结果" align="center" prop="repairResult" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            :icon="View"
            @click="handleView(scope.row)"
            v-hasPermi="['qep:repair-order:query']"
          >查看</el-button>
          <el-button
            type="primary"
            link
            :icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qep:repair-order:edit']"
          >修改</el-button>
          <el-button
            type="primary"
            link
            :icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qep:repair-order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修工单对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="缺陷记录ID" prop="defectRecordId">
          <el-input v-model="form.defectRecordId" placeholder="请输入缺陷记录ID" />
        </el-form-item>
        <el-form-item label="工单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入工单编号" />
        </el-form-item>
        <el-form-item label="工单状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择工单状态">
            <el-option label="待处理" value="待处理" />
            <el-option label="处理中" value="处理中" />
            <el-option label="已完成" value="已完成" />
            <el-option label="已取消" value="已取消" />
          </el-select>
        </el-form-item>
        <el-form-item label="维修类型" prop="repairType">
          <el-select v-model="form.repairType" placeholder="请选择维修类型">
            <el-option label="常规维修" value="常规维修" />
            <el-option label="紧急维修" value="紧急维修" />
            <el-option label="预防性维修" value="预防性维修" />
          </el-select>
        </el-form-item>
        <el-form-item label="维修级别" prop="repairLevel">
          <el-select v-model="form.repairLevel" placeholder="请选择维修级别">
            <el-option label="轻度" value="轻度" />
            <el-option label="中度" value="中度" />
            <el-option label="重度" value="重度" />
          </el-select>
        </el-form-item>
        <el-form-item label="分配给" prop="assignTo">
          <el-input v-model="form.assignTo" placeholder="请输入分配人员" />
        </el-form-item>
        <el-form-item label="计划日期" prop="plannedDate">
          <el-date-picker clearable
            v-model="form.plannedDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择计划日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际日期" prop="actualDate">
          <el-date-picker clearable
            v-model="form.actualDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择实际日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修结果" prop="repairResult">
          <el-input v-model="form.repairResult" type="textarea" placeholder="请输入维修结果" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="submitLoading" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listRepairOrder, getRepairOrder, addRepairOrder, updateRepairOrder, delRepairOrder, getRepairOrderByDefectRecordId } from "@/api/qep/repairOrder";
import { Search, Refresh, Plus, Edit, Delete, View, Download } from '@element-plus/icons-vue';

export default {
  name: "RepairOrder",
  components: {},
  data() {
    return {
      // 图标
      Search,
      Refresh,
      Plus,
      Edit,
      Delete,
      View,
      Download,
      // 遮罩层
      loading: true,
      // 提交按钮加载状态
      submitLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修工单表格数据
      repairOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        defectRecordId: null,
        status: null,
        assignTo: null,
        repairType: null,
        repairLevel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        defectRecordId: [
          { required: true, message: "缺陷记录ID不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "工单编号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "工单状态不能为空", trigger: "change" }
        ],
        repairType: [
          { required: true, message: "维修类型不能为空", trigger: "change" }
        ],
        repairLevel: [
          { required: true, message: "维修级别不能为空", trigger: "change" }
        ],
        assignTo: [
          { required: true, message: "分配给不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维修工单列表 */
    getList() {
      this.loading = true;
      listRepairOrder(this.queryParams).then(response => {
        this.repairOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        defectRecordId: null,
        repairType: null,
        repairLevel: null,
        assignTo: null,
        plannedDate: null,
        actualDate: null,
        repairResult: null,
        status: "待处理",
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修工单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids[0]
      getRepairOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修工单";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id
      getRepairOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看维修工单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const isUpdate = this.form.id != null;
          const actionText = isUpdate ? "修改" : "新增";
          
          this.$confirm(`确认${actionText}该维修工单?`).then(() => {
            this.submitLoading = true;
            const request = isUpdate ? updateRepairOrder(this.form) : addRepairOrder(this.form);
            
            request.then(response => {
              this.$message({
                message: `${actionText}成功`,
                type: "success"
              });
              this.open = false;
              this.getList();
            }).finally(() => {
              this.submitLoading = false;
            });
          }).catch(() => {});
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除维修工单编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delRepairOrder(ids);
      }).then(() => {
        this.getList();
        this.$message({
          message: "删除成功",
          type: "success"
        });
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qep/repair-order/export', {
        ...this.queryParams
      }, `维修工单_${new Date().getTime()}.xlsx`)
    }
  }
};
</script> 